package cn.wit.summit.chwan.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.request.TreviFountainRequest;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.models.response.TreviFountainResponse;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.AccountBean;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.CurrencyPromptDialog;
import retrofit2.Retrofit;

/**
 * 许愿池
 * Created by Alan on 2018/4/20.
 */

public class TreviFountainV2Activity extends MyActivity {

    @Bind(R.id.treviFountionIv)
    SimpleDraweeView treviFountionIv;
    @Bind(R.id.treviFountionEt)
    EditText treviFountionEt;
    @Bind(R.id.treviFountionRightRewardTx)
    TextView treviFountionRightRewardTx;
    @Bind(R.id.treviFountionRightLl)
    LinearLayout treviFountionRightLl;
    @Bind(R.id.treviFountionTopIv)
    ImageView treviFountionTopIv;

    private int uid;
    private String account;
    private String token;

    private RetrofitManager retrofitManager;
    private Retrofit retrofit;
    private ApiUtil api;

    private int topHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trevi_fountain_v2);
        ButterKnife.bind(this);

        hideTitleBar();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            topHeight = getStateBarHeight();
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, topHeight);
            treviFountionTopIv.setLayoutParams(params);
            treviFountionTopIv.setVisibility(View.VISIBLE);
        }

        retrofitManager = RetrofitManager.getInstance(mContext);
        retrofit = retrofitManager.getRetrofit();
        api = retrofit.create(ApiUtil.class);
    }

    private void submitTheMessage() {
        AccountBean accountBean = AccountUtil.getInstance().getAccountData();
        if (accountBean == null) {
            IntentUtil.getInstance().goLoginActivity(TreviFountainV2Activity.this);
            return;
        }
        uid = accountBean.getUid();
        account = accountBean.getAccount();
        token = accountBean.getToken();
        String name = "";
        String detail = treviFountionEt.getText().toString().trim();
//        if (StringUtils.isEmpty(name)) {
//            showToast(getResources().getString(R.string.hint_gamename_null_str));
//            return;
//        }
        if (StringUtils.isEmpty(detail)) {
            showToast(getResources().getString(R.string.hint_gameinfo_null_str));
            return;
        }
        sendMessage(name, detail);
    }

    private void sendMessage(String name, String detail) {
        showLoading(getResources().getString(R.string.trevi_sub_str));
        TreviFountainRequest mTreviFountainRequest = retrofitManager.sendTreviGamesRequest(uid + "", account, token, name, detail);
        api.sendTreviGames(mTreviFountainRequest).enqueue(new MyCallback<ResponseBase<TreviFountainResponse>>() {
            @Override
            public void onSuccess(ResponseBase<TreviFountainResponse> model) {
                cancelLoading();
                showDialog();
            }

            @Override
            public void onFailure(int code, String msg) {
                cancelLoading();
                showToast(msg);
            }
        });
    }

    private void showDialog() {
        final CurrencyPromptDialog dialog = new CurrencyPromptDialog(mContext);
        dialog.show();
        dialog.setTitle(getResources().getString(R.string.trevi_dialog_title_str));
        dialog.setMsg(getResources().getString(R.string.trevi_dialog_msg_str));
        dialog.setCancleBtn(getResources().getString(R.string.trevi_dialog_bt_str), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                treviFountionEt.setText("");
                treviFountionEt.clearFocus();
            }
        });
    }

    @OnClick({R.id.treviFountionLeftBt, R.id.treviFountionRightLl, R.id.treviFountionBackIv
            , R.id.treviFountionCheckTx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.treviFountionBackIv:
                finish();
                break;
            case R.id.treviFountionLeftBt:
                hideSoftInput(view);
                submitTheMessage();
                break;
            case R.id.treviFountionRightLl:
                if (AccountUtil.getInstance().getUidNotToLogin(mContext) == 0) return;
                IntentUtil.getInstance().goShareWebActivity(mContext, MyApplication.cfgPlug.getValue_game_upload_url_v3());
                break;
            case R.id.treviFountionCheckTx:
                Intent intent = new Intent(mContext, TreviFountainListActivity.class);
                intent.putExtra("isFormCreateTrevi", true);
                mContext.startActivity(intent);
                break;
        }
    }
}
